package com.koko.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.koko.constant.FileNameConstant;
import com.koko.mapper.NewsMapper;
import com.koko.news.dto.NewsDTO;
import com.koko.news.dto.NewsPageQueryDTO;
import com.koko.news.entity.News;
import com.koko.service.NewsService;
import com.koko.utils.IOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Value("${koko.path.publisher}")
    private String basePath;
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public Page<News> pageQuery(NewsPageQueryDTO newsPageQueryDTO) {
        PageHelper.startPage(newsPageQueryDTO.getPage(), newsPageQueryDTO.getPageSize());
        return newsMapper.pageQuery(newsPageQueryDTO);
    }

    @Override
    public void save(NewsDTO newsDTO) {
        News news = new News();
        BeanUtils.copyProperties(newsDTO, news, "content");
        // 1. 给content文件名赋予初始值
        news.setContent(FileNameConstant.NEWS_INITIAL_FILE_NAME);
        // 2. 先插入数据库获取主键
        newsMapper.insert(news);
        // 3. 将主键作为文件夹名字
        String folderPath = basePath + news.getPublisherId() + "/" + FileNameConstant.NEWS_FOLDER_NAME + "/" + news.getId();
        // 4. 将内容写入txt文件
        IOUtil.createDirectory(folderPath);
        IOUtil.saveStringToTxt(newsDTO.getContent(), folderPath + "/", FileNameConstant.NEWS_INITIAL_FILE_NAME);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            // 抽出待删除新闻信息
            News newsToDelete = newsMapper.getById(id);
            // 1. 先删除数据库
            newsMapper.deleteById(id);
            // 2. 再删除文件夹
            IOUtil.deleteFileOrDirectory(basePath +
                    newsToDelete.getPublisherId() + "/" +
                    FileNameConstant.NEWS_FOLDER_NAME + "/" +
                    newsToDelete.getId());
        }
    }

    @Override
    public void update(NewsDTO newsDTO) {
        News newsInDB = newsMapper.getById(newsDTO.getId());
        if (newsInDB.getPublisherId().equals(newsDTO.getPublisherId())) {
            // 发行商没有变化，只改变文件中新闻内容
            String folderPath = basePath + newsDTO.getPublisherId() + "/" + FileNameConstant.NEWS_FOLDER_NAME;
            IOUtil.saveStringToTxt(newsDTO.getContent(),
                    folderPath + "/" + newsInDB.getId() + "/",
                    newsInDB.getContent());
        } else {
            // 发行商发生变化
            // 1. 先删除原本的发行商内的文件夹
            String OldFolderPath = basePath + newsInDB.getPublisherId() + "/" + FileNameConstant.NEWS_FOLDER_NAME + "/" + newsInDB.getId();
            IOUtil.deleteFileOrDirectory(OldFolderPath);
            // 2. 再将新的内容存入文件夹
            // 创建新闻文件夹
            String newFolderPath = basePath + newsDTO.getPublisherId() + "/" + FileNameConstant.NEWS_FOLDER_NAME + "/" + newsInDB.getId();
            IOUtil.createDirectory(newFolderPath);
            String newFileName = IOUtil.saveStringToTxt(newsDTO.getContent(), newFolderPath + "/");
            // 3. 将新文件名替换掉旧文件名
            newsInDB.setContent(newFileName);
        }
        BeanUtils.copyProperties(newsDTO, newsInDB, "content");
        newsMapper.update(newsInDB);
    }

    @Override
    public News getById(Long id) {
        News news = newsMapper.getById(id);
        // 从txt文件中读取文件
        news.setContent(IOUtil.readHtmlContentFromTxt(basePath +
                news.getPublisherId() + "/" +
                FileNameConstant.NEWS_FOLDER_NAME + "/" +
                news.getId() + "/" +
                news.getContent()));
        return news;
    }
}
